package com.mmlc.meimei.lichengkongjian;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.classify.ProductInfoActivity;
import com.mmlc.utils.AppUtils;
import com.mmlc.utils.SPUtils;
import com.singulariti.deepshare.DeepShare;
import com.singulariti.deepshare.listeners.DSInappDataListener;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

/**
 * Created by Administrator on 2016/8/9.
 */
public class WelcomeActivity extends InterActivity implements DSInappDataListener {

    private boolean from;

    @Override
    protected int initContentView() {
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_welcome;
    }

    @Override
    protected void initView() {
        // 请直接复制下一行代码，粘贴到相应位置
        DeepShare.init(this, "12fce89f8dec4330", this);
        x.task().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!from) {
                    int appNo = (int) SPUtils.get(context, "appNo", -1);
                    if (appNo != AppUtils.getVersionCode(WelcomeActivity.this)) {
                        mHandler.sendEmptyMessageDelayed(1, 2000);
                    } else {
                        mHandler.sendEmptyMessageDelayed(1, 2000);
                    }
                }
            }
        }, 1000);
    }

    Handler mHandler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            SPUtils.put(context, "appNo", AppUtils.getVersionCode(WelcomeActivity.this));
            switch (msg.what) {
                case 0:/** to guide */
                    startActivity(new Intent(activity, GuideActivity.class));
                    break;
                case 1:/** to main */
                    startActivity(new Intent(activity, MainActivity.class));
                    break;
                case 2:/** to login */

                    break;
            }
            finish();
        }
    };

    @Override
    protected void initEvent() {

    }

    @Override
    public void onNewIntent(Intent intent) {
        this.setIntent(intent);
    }

    @Override
    public void onStop() {
        super.onStop();
        DeepShare.onStop();//停止DeepShare
    }

    /**
     * linkTyte = 0;//来源分享,-1:app其他界面启动
     */
    @Override
    public void onInappDataReturned(JSONObject params) {
        from=true;
        if (params != null) {
            Log.d("PARAMS", params.toString());
            if (TextUtils.equals("{}", params.toString())) {
                int appNo = (int) SPUtils.get(context, "appNo", -1);
                if (appNo != AppUtils.getVersionCode(this)) {
                    mHandler.sendEmptyMessageDelayed(1, 2000);
                } else {
                    mHandler.sendEmptyMessageDelayed(1, 2000);
                }
                return;
            }
            try {
                String id = params.getString("goods_id");
                // *注意*: 下面一行替换成， 调用应用自己的接口跳转到分享时页面
            /*goToTarget(params);*/
                String goodsId = id;
                if (!TextUtils.isEmpty(goodsId)) {
                    Intent intent = new Intent(this, ProductInfoActivity.class);
                    intent.putExtra("goods_id", goodsId);
                    intent.putExtra("linkTyte", 0);

                    startActivity(intent);
                    finish();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            int appNo = (int) SPUtils.get(context, "appNo", -1);
            if (appNo != AppUtils.getVersionCode(this)) {
                mHandler.sendEmptyMessageDelayed(1, 2000);
            } else {
                mHandler.sendEmptyMessageDelayed(1, 2000);
            }
        }

    }

    @Override
    public void onFailed(String s) {

    }
}
